<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use OpenSearch\Client\OpenSearchClient;
use App\Models\News\News;
use App\Models\Artists\JoinArtist;
use App\Models\Artists\Artist;
use App\Models\Auctions\Zhuanchang;
use App\Models\Auctions\Art;
use App\Helper\OpenSearch\Search;
use App\Models\OpenSearchLog;
use OpenSearch\Client\DocumentClient;
class Test extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test:name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        $Search = new Search();
        $list = JoinArtist::orderBy('id', 'asc')->get();
        for ($i=0; $i < 50; $i++) { 
            # code...
            $data = [];
            foreach ($list->forPage($i,100) as $key => $value) {
                # code...
                $OpenSearchLog = OpenSearchLog::where('item_type',2)->where('item_id', $value->id)->first();
                if($OpenSearchLog)
                {
                    $data[] = [
                            'cmd' => 'add',
                            'fields' => $Search->data_artists($value, $OpenSearchLog->id)
                    ];
                }
            }
            if($data)
            {
                $json = json_encode($data);
                $documentClient = new DocumentClient($Search->client);
                $ret = $documentClient->push($json, $Search->appName, $Search->tableName);
                // $arr = json_decode($ret);
                var_dump($ret);
                // return $ret;
            }
            // dd($data);
        }
        
    }
    /*
    public function handle()
    {
        $Search = new Search();
        // $Search->artistku();
        // dd(1);
        // dd($Search->accessKeyId);
        $OpenSearchLog = OpenSearchLog::where('item_type',2)->get();
		foreach($OpenSearchLog as $row)
		{
			
		}
        for($i=1;$i<40;$i++){
			$list = OpenSearchLog::where('item_type',2)->offset(($i-1)*100)->limit(100)->orderBy('id', 'asc')->get();
            if($list->isEmpty())
            {
                echo 'ok';
                break;
            }
            $data = [];
            // dd($list);
            // $id = ($Search->get_last_open_search_log());
            foreach($list as $key=>$row)
            {
                $rows = JoinArtist::find($row->item_id);
                if(!$rows)continue;
                $data[] = [
                        'cmd' => 'update',
                        'fields' => $Search->data_artists($rows, $row->id)
                ];
                
            }
			// dd($data);
            // cache(['i123dsss'=>$row->id],10);
            // echo $row->id;
            if($data)
            {
                $json = json_encode($data);
                $documentClient = new DocumentClient($Search->client);
                $ret = $documentClient->push($json, $Search->appName, $Search->tableName);
                var_export($ret);
                // return $ret;
            }

        }


        // $Search = new Search();
		// return $Search->zc();
        // $t = 50;
        // for( $i=1; $i<$t; $i++ )
        // {
        //     $Search->zc();
        //     sleep(1);
        // }
		
		
    }

    public function chongfu()
    {
        $Zhuanchang = Zhuanchang::where('id', ">", 23283)->get();
        foreach ($Zhuanchang as $key => $value) {
            # code...
            $list = Art::where('zc_id', $value->id)->select(\DB::raw('count(*) as num, listorder'))->groupBy('listorder')->get();
            
            foreach ($list as $key => $row) {
                # code...

                if($row->num >1)
                {
                    echo $value->id;
                    echo $row->listorder;
                // dd($row);
                    (Art::where('zc_id', $value->id)->where('listorder', $row->listorder)->limit($row->num-1)->delete());
                    ($row->forceDelete());
                }

            }
            echo $value->id.chr(10);
            // dd($value);
            
        }
    }
	
	
	public function handle()
    {
        $Search = new Search();
        // $Search->artistku();
        // dd(1);
        // dd($Search->accessKeyId);
        
        for($i=1;$i<86;$i++){
            $list = JoinArtist::where('id', 30)->orderBy('id', 'asc')->get();
            $ids = ($Search->get_last_open_search_log_type(5));
            dd($ids);
            $list = Artist::where('content', '!=', ' ')->whereNotNull('content')->where('id', '>',$ids)->orderBy('id', 'asc')->limit(100)->get();
            if($list->isEmpty())
            {
                echo 'ok';
                break;
            }
            $data = [];
            // dd($list);
            $id = ($Search->get_last_open_search_log());
            foreach($list as $key=>$row)
            {
                // $rows = JoinArtist::find($row->item_id);
                // if(!$rows)continue;
                $data[] = [
                        'cmd' => 'ADD',
                        'fields' => $Search->data_artistsku($row, $id+$key+1)
                ];
                
            }
            // cache(['i123dsss'=>$row->id],10);
            echo $row->id;
            if($data)
            {
                $json = json_encode($data);
                $documentClient = new DocumentClient($Search->client);
                $ret = $documentClient->push($json, $Search->appName, $Search->tableName);
                var_export($ret);
                // return $ret;
            }

        }


        // $Search = new Search();
		// return $Search->zc();
        // $t = 50;
        // for( $i=1; $i<$t; $i++ )
        // {
        //     $Search->zc();
        //     sleep(1);
        // }
		
		
    }*/
	
	
}
